package com.marcnuri.android.widget;

import mydebug.MyDebug;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public abstract class ListAdapter<T, V extends ViewHolder<T>> extends BaseAdapter {
	protected Context context;
	private ArrayList<T> listItems = new ArrayList<T>();
	private LayoutInflater layoutInflater;
	private int mViewId;

	public ListAdapter(Context context, int viewid, ArrayList<T> listItems) {
		MyDebug.beginMethod("ListAdapter:public ListAdapter<T,");
		layoutInflater = LayoutInflater.from(context);
		mViewId = viewid;
		this.listItems = listItems;
		this.context = context;
		MyDebug.endMethod();
	}

	public int getCount() {
		MyDebug.beginMethod("getCount:int ListAdapter<T,");
		{
			int giatritrave = listItems.size();
			MyDebug.endMethod();
			return giatritrave;
		}
	}

	public T getItem(int position) {
		MyDebug.beginMethod("getItem:T ListAdapter<T,");
		{
			T giatritrave = listItems.get(position);
			MyDebug.endMethod();
			return giatritrave;
		}
	}

	public long getItemId(int position) {
		MyDebug.beginMethod("getItemId:long ListAdapter<T,");
		{
			long giatritrave = position;
			MyDebug.endMethod();
			return giatritrave;
		}
	}

	@SuppressWarnings("unchecked")
	public View getView(int position, View view, ViewGroup parent) {
		MyDebug.beginMethod("@SuppressWarnings: ListAdapter<T,");
		V holder;
		if (view == null) {
			view = layoutInflater.inflate(mViewId, null);
			holder = createHolder(view);
			view.setTag(holder);
		} else {
			holder = (V) view.getTag();
		}
		holder.data = getItem(position);
		bindHolder(holder);
		{
			View giatritrave = view;
			MyDebug.endMethod();
			return giatritrave;
		}
	}

	protected abstract V createHolder(View v);

	protected abstract void bindHolder(V h);
}
